#include "h_cc2640r2f_simpletime.h"

#include <ti/sysbios/knl/Clock.h>



static Clock_Handle h_cc2640r2f_simpletimedelayhandle = NULL;
static unsigned int h_cc2640r2f_simpletimedelaycount = 0U;

//static Clock_Struct h_cc2640r2f_simpletimedelayStruct;
//static unsigned int h_cc2640r2f_simpletimedelaycount = 0U;

unsigned int H_CC2640R2F_SimpleTime_GetSystickms(void)
{
     return (Clock_getTicks() / 100);
}

/*
void H_CC2640R2F_SimpleTime_Delay(unsigned int dly)
{
    unsigned int timenow = H_CC2640R2F_SimpleTime_GetSystickms();
    while(H_CC2640R2F_SimpleTime_GetSystickms() - timenow > dly)
    {
        asm("nop");
    }
}
*/

//static Clock_Handle H_CC2640R2F_SimpleTime_TimerCreate(unsigned int timeout, \
//                                                  unsigned int period, \
//                                                    h_cc2640r2f_simpletime_timercallbackFxn timercallbackFxn)
//{
//    unsigned int timeoutTick = timeout * 1000 / Clock_tickPeriod;
//    Clock_Handle clock_handle;
//
//    Clock_Params params;
//    Clock_Params_init(&params);
//
//    params.period = period * 1000 / Clock_tickPeriod;
//    params.startFlag = 0;   //Do not start immediately
//
//    clock_handle = Clock_create(timercallbackFxn, timeoutTick, &params, NULL);
//    return clock_handle;
//}



signed char H_CC2640R2F_SimpleTime_TimerStart(Clock_Handle clock_h)
{
    if(clock_h == NULL)
    {
        return (-1);
    }
    Clock_start(clock_h);
    return 0;
}

signed char H_CC2640R2F_SimpleTime_TimerStop(Clock_Handle clock_h)
{
    if(clock_h == NULL)
    {
        return (-1);
    }
    Clock_stop(clock_h);
    return 0;
}

signed char H_CC2640R2F_SimpleTime_TimerDelete(Clock_Handle *clock_h)
{
    if(clock_h == NULL)
    {
        return (-1);
    }
    Clock_delete(clock_h);
    return 0;
}


//#include "util.h"
//
//static void h_cc2640r2f_simpletimedelaycallbackFxn(UArg arg0)
//{
//
//}
//
//signed char H_CC2640R2F_SimpleTime_Delayms_TimerCreate(void)
//{
//    Util_constructClock(&h_cc2640r2f_simpletimedelayStruct, \
//                            h_cc2640r2f_simpletimedelaycallbackFxn,
//                                1, 0, false, NULL);
//    return 0;
//}
//
//void H_CC2640R2F_SimpleTime_Delay(unsigned int dly)
//{
//    h_cc2640r2f_simpletimedelaycount = dly;
//    Util_startClock(&h_cc2640r2f_simpletimedelayStruct);
//    for()
//}


static void h_cc2640r2f_simpletimedelaycallbackFxn(UArg arg0)
{
    if(h_cc2640r2f_simpletimedelayhandle != NULL)
    {
        if(h_cc2640r2f_simpletimedelaycount == 0U)
        {
            H_CC2640R2F_SimpleTime_TimerStop(h_cc2640r2f_simpletimedelayhandle);
        }
        else
        {
            h_cc2640r2f_simpletimedelaycount--;
        }
    }
}

signed char H_CC2640R2F_SimpleTime_Delayms_TimerCreate(void)
{
//    h_cc2640r2f_simpletimedelayhandle = H_CC2640R2F_SimpleTime_TimerCreate(1, 1, \
                                                                               h_cc2640r2f_simpletimedelaycallbackFxn);
    unsigned int timeoutTick = 1000 / Clock_tickPeriod;

    Clock_Params params;
    Clock_Params_init(&params);

    params.period = timeoutTick;
    params.startFlag = 0;   //Do not start immediately

    h_cc2640r2f_simpletimedelayhandle = Clock_create(h_cc2640r2f_simpletimedelaycallbackFxn, timeoutTick, &params, NULL);

    if(NULL == h_cc2640r2f_simpletimedelayhandle)
    {
        return (-1);
    }
    return 0;
}

void H_CC2640R2F_SimpleTime_Delay(unsigned int dly)
{
    h_cc2640r2f_simpletimedelaycount = dly;
    H_CC2640R2F_SimpleTime_TimerStart(h_cc2640r2f_simpletimedelayhandle);
    while(h_cc2640r2f_simpletimedelaycount)
    {
      ;
    }
}

void H_CC2640R2F_SimpleTime_Around_Delay(unsigned int dly)
{
    unsigned int i,j;
    for(i = 0;i < dly; i++)
    {
        for(j = 0; j < 12000; j++)
        {
            asm(" nop ");
        }
    }
}

